﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_ProductsBll.cs
文件名称：ProductsBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010/12/17
作　　者：
内容摘要：表[Purchase]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.Collections;
using System.Data;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.FrameWork;
using System.IO;
using Eis51.CampanyCMS.Model.Bus.QueryO;
using MoreUser.Bussiness;
using Eis51.CampanyCMS.Access.Bus;
using System.Web.UI.WebControls;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
    /// Purchase的BLL 自定义方法
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class PurchaseBLL : BaseBLL<Purchase>
    {


        string symbolf = "";
        string symbol = "";
        public void TreeListData(string parentID, string CreateBy, DataTable allTable, DataTable NewTable)
        {
            DataRow[] dataArr = allTable.Select("CreateBy='" + CreateBy+"'");
            int total = dataArr.Length;
            for (int i = 0; i < total; i++)
            {
                if ((i + 1) == total)
                    symbol = "└";
                else
                    symbol = "├";
                DataRow newRow = NewTable.NewRow();
                newRow["Parentid"] = "0";
                newRow["title"] = symbolf + symbol + dataArr[i]["title"].ToString();
                newRow["seqno"] = dataArr[i]["seqno"].ToString();
                NewTable.Rows.Add(newRow);
                if ((i + 1) == total)
                    symbolf += "　";
                else
                    symbolf += "│";
                //TreeListData("0", CreateBy, allTable, NewTable);
                symbolf = symbolf.Substring(0, symbolf.Length - 1);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listControl"></param>
        public static void BindDropListTreeType2(ListControl listControl, string CreateBy, int Licd,string noHave)
        {
            string sql = string.Format("select * from Products where CreateBy='{0}'  and Licd={1} and Seqno not in({2})", CreateBy, Licd, noHave);
            DataTable dt = PurchaseBLL.GetInstance().GetDataTable(sql);
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("Parentid");
            dtNew.Columns.Add("title");
            dtNew.Columns.Add("seqno");
            PurchaseBLL ptree = new PurchaseBLL();
            ptree.TreeListData("0", CreateBy, dt, dtNew);

            PurchaseBLL bll = new PurchaseBLL();
            listControl.DataSource = dtNew;
            listControl.DataTextField = "title";
            listControl.DataValueField = "seqno";
            listControl.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listControl"></param>
        public static void BindDropListTreeType(ListControl listControl, string CreateBy, int Licd)
        {
            string sql = string.Format("select * from Products where CreateBy='{0}'  and Licd={1} ", CreateBy, Licd);
            DataTable dt = PurchaseBLL.GetInstance().GetDataTable(sql);
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("Parentid");
            dtNew.Columns.Add("title");
            dtNew.Columns.Add("seqno");
            PurchaseBLL ptree = new PurchaseBLL();
            ptree.TreeListData("0", CreateBy, dt, dtNew);

            PurchaseBLL bll = new PurchaseBLL();
            listControl.DataSource = dtNew;
            listControl.DataTextField = "title";
            listControl.DataValueField = "seqno";
            listControl.DataBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public IList Search(PurchaseQuery query)
        {
            string sql = "select * from Purchase where 1=1 ";
            if (query.Type != "")
            {
                sql += " and   (type={0} or type in (select seqno from Products_Type where Parentid={0}) )";
                sql = string.Format(sql, query.Type);
            }
            if (query.ProductName != "")
            {
                sql += " and (Title like '%{0}%'   )";
                sql = string.Format(sql, query.ProductName.Trim().Replace("'", ""));
            }
            if (query.CreateBy != "")
            {
                sql += " and CreateBy = '{0}' ";
                sql = string.Format(sql, query.CreateBy);
            }
            sql += " order by LasteUpdateDate desc ";
            DataTable dt = this.GetDataTable(sql);
            return Purchase.Dt2Objs(dt, Purchase.ColumnNameEnum.DBName);
        }


        public DataTable Search(PurchaseQuery query, int first, int pagesize)
        {
            string sql = "select *,year(createdate) &'-'& month(createdate)&'-'& day(createdate) as PRODUCTDATE  from Purchase where 1=1 ";
            if (query.Type != "")
            {
                sql += " and   (type={0} or  type in (select Mychildid  from MyAllChild where mainid={0} ) )";
                sql = string.Format(sql, query.Type);
            }
            if (query.ProductName != "")
            {
                sql += " and (Title like '%{0}%'   )";
                sql = string.Format(sql, query.ProductName.Trim().Replace("'", ""));
            }

            sql += " and CreateBy = '{0}' ";
            sql = string.Format(sql, query.CreateBy);

            sql += " order by LasteUpdateDate desc ";

            return BaseSearchConditonBLL.GetDataForPage(sql, first, pagesize);
        }


        public int SearchCount(PurchaseQuery query)
        {
            string sql = "select count(*) as count1 from Purchase where 1=1 ";
            if (query.Type != "")
            {
                sql += " and   (type={0} or type in (select Mychildid  from MyAllChild where mainid={0} ))";
                sql = string.Format(sql, query.Type);
            }
            if (query.ProductName != "")
            {
                sql += " and (Title like '%{0}%'   )";
                sql = string.Format(sql, query.ProductName.Trim().Replace("'", ""));
            }
            sql += " and CreateBy = '{0}' ";
            sql = string.Format(sql, query.CreateBy);
            DataTable dt = this.GetDataTable(sql);
            return int.Parse(dt.Rows[0]["count1"].ToString());
        }



        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">条件＝空时，返回一个空集合</param>
        /// <returns></returns>
        public IList Search(string scondition)
        {
            if (scondition == string.Empty)
            {
                scondition = "1<>1";
            }
            string sql = "select * from Purchase where 1=1  and " + scondition;
            DataTable dt = this.GetDataTable(sql);
            return Purchase.Dt2Objs(dt, Purchase.ColumnNameEnum.DBName);
        }

        /// <summary>
        ///   提交新增/更新
        /// </summary>
        /// <param name="p"></param>
        /// <param name="fileList"></param>
        /// <returns></returns>
        public int Submit(Purchase p, IList fileList, IList itCapabilityCurve)
        {

            int i = 0;
            if (p.Seqno == 0)
            {
                p.Createdate = DateTime.Now;
                p.Createby = AppContext.CurrentAdministrator.UserName;
                p.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
                p.Lasteupdatedate = DateTime.Now;
                i = Insert(p);
                if (string.IsNullOrEmpty(p.Code))
                {
                    p.Code = p.Seqno.ToString();
                    this.Update(p);
                }
                FileListBLL.GetInstance().ProductImgSubMit(p.Seqno.ToString(), fileList, FileType.Product);
                //  bll.ProductImgSubMit(p.Seqno.ToString(),itCapabilityCurve, FileType.CapabilityCurve);
            }
            else
            {
                p.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
                p.Lasteupdatedate = DateTime.Now;
                GetInstance().Update(p);
                i = p.Seqno;
                //对比数据库的图片
                string haslistids = "";
                foreach (FileList tmp in fileList)
                {
                    haslistids += "{0},";
                    haslistids = string.Format(haslistids, tmp.Seqno);
                }
                haslistids = haslistids.Trim(',');
                FileListBLL.GetInstance().ProductImgSubMit(p.Seqno.ToString(), fileList, FileType.Product);
                // bll.ProductImgSubMit(p.Seqno.ToString(),itCapabilityCurve, FileType.CapabilityCurve);
            }

            BuildPageBLL.CreateHtml(RunType.Product, p);
            return i;

        }


        /// 删除产品
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public bool Delete(string productid)
        {

            IList fileList = FileListBLL.GetInstance().GetFilesByProductId(productid, FileType.Product);
            //删除对应的相片
            foreach (FileList tmp in fileList)
            {
                string imgp = Path.Combine(AppContext.UserFileFoder, "bigimg");
                imgp = Path.Combine(imgp, tmp.FileName);
                imgp = System.Web.HttpContext.Current.Server.MapPath(imgp);
                if (File.Exists(imgp))
                {
                    File.Delete(imgp);
                }
                imgp = Path.Combine(AppContext.UserFileFoder, "smallimg");
                imgp = Path.Combine(imgp, tmp.FileName);
                imgp = System.Web.HttpContext.Current.Server.MapPath(imgp);
                if (File.Exists(imgp))
                {
                    File.Delete(imgp);
                }


            }
            Purchase obj = PurchaseBLL.GetInstance().Get(new Purchase.Key(int.Parse(productid)));
            this.Delete(obj);
            return true;
        }

        /// <summary>
        /// 图片
        /// </summary>
        /// <param name="ProductId"></param>
        /// <returns></returns>
        public static string GetOnePic(string ProductId)
        {

            IList fileList = FileListBLL.GetInstance().GetFilesByProductId(ProductId, FileType.Product);
            string filename = "";

            if (fileList.Count > 0)
            {
                filename = AppContext.ImgUpLoadShowPath + (fileList[0] as FileList).FileName;
            }
            return filename;
        }

        /// <summary>
        /// 名称
        /// </summary>
        /// <param name="ProductId"></param>
        /// <returns></returns>
        public static string GetProductName(string ProductId)
        {

            Purchase product = PurchaseBLL.GetInstance().Get(new Purchase.Key(int.Parse(ProductId)));
            if (product != null)
            {
                return product.Title;
            }
            return "";
        }

    }

    #region 代码自动生成
    /// <summary>
    /// Purchase的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class PurchaseBLL : BaseBLL<Purchase>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: PurchaseBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected PurchaseBLL()
        {
        }
        private static volatile PurchaseBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 PurchaseBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static PurchaseBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(PurchaseBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new PurchaseBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>Purchase</returns>
        public Purchase Get(Purchase.Key key)
        {

            return PurchaseDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return PurchaseDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(Purchase obj)
        {
            int result = 0;
            try
            {
                result = PurchaseDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(Purchase obj)
        {
            PurchaseDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(Purchase obj)
        {
            bool result = PurchaseDao.GetInstance().DeleteByID(new Purchase.Key(obj.Seqno));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}